//
//  UIViewController+Tracking.m
//  runtime
//
//  Created by soulghost on 6/1/2016.
//  Copyright © 2016 soulghost. All rights reserved.
//

#import "UIViewController+Tracking.h"
#include <objc/runtime.h>

@implementation UIViewController (Tracking)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        
        SEL oriSEL = @selector(viewWillAppear:);
        SEL swiSEL = @selector(track_viewWillAppear:);
        
        Method oriMethod = class_getInstanceMethod(class, oriSEL);
        Method swizMethod = class_getInstanceMethod(class, swiSEL);
        
        BOOL didAddMethod = class_addMethod(class, oriSEL, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
        if (didAddMethod) {
            class_replaceMethod(class, swiSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
        }else{
            method_exchangeImplementations(oriMethod, swizMethod);
        }
        
    });
}


- (void)track_viewWillAppear:(BOOL)animated{
//    self.view.backgroundColor = [UIColor grayColor];
    [self track_viewWillAppear:animated];
    NSLog(@"viewWillAppear:%s %@",__func__,self);
}

@end
